include(FetchContent)

FetchContent_Declare(
    pybind11
    GIT_REPOSITORY https://gitee.com/mirrors/pybind11
    GIT_TAG v3.0.0
)

FetchContent_MakeAvailable(pybind11)

pybind11_add_module(_core ndarray.cpp)

# 链接库
target_link_libraries(_core PRIVATE
    tinyndarray
    pybind11::module
)

# 设置输出目录
set_target_properties(_core PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/python/tinynumpy
    PREFIX ""
    SUFFIX "${PYTHON_MODULE_EXTENSION}"
)

find_package(Python COMPONENTS Interpreter REQUIRED)


add_custom_target(copy_py_src
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/tinynumpy python/tinynumpy
    DEPENDS _core
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    COMMENT "Running unit tests with unittest..."
)

add_custom_target(pytest
    COMMAND ${Python_EXECUTABLE} -m pytest -s ${CMAKE_SOURCE_DIR}/pytest
    DEPENDS _core copy_py_src
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    COMMENT "Running unit tests with unittest..."
)


set_target_properties(pytest PROPERTIES
    ENVIRONMENT "PYTHONPATH=${CMAKE_BINARY_DIR}/python:$ENV{PYTHONPATH}"
)
